# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
express = require 'express'
pgsql   = require 'pg'
uuid    = require 'uuid'
router = express.Router()
module.exports = router


router.all '/', (req, res, next) ->
    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    where_clause = "(
        name like $1 or notes like $1
    )"
    # 查询记录总数
    await client.query "select count(*) as count from pay_trctl_t
        where #{ where_clause }", [
            '%' + res.locals.keyword + '%'
        ], defer err, result
    if err then done(client); return next(err)
    res.locals.total = result.rows[0].count

    # 查询当前页面中显示的记录
    await client.query "select * from pay_trctl_t
        where #{ where_clause }
        order by serial offset $2::int limit $3::int", [
            '%' + res.locals.keyword + '%',
            res.locals.offset,
            res.locals.page_rows
        ], defer err, result
    if err then done(client); return next(err)
    done()
    res.render 'trctl_t/index', records : result.rows


router.get '/new', (req, res, next) ->
    res.locals.breadcrumbs.push 'title': '新增交易控制模板'
    res.render 'trctl_t/new', record: {}


router.post '/new', (req, res, next) ->
    for k in [ 'name' ]
        return next(new Error("请求缺少参数[#{k}]")) if not req.body[k]

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "insert into pay_trctl_t (
            uuid, name, trctl, notes
        ) values (
            $1, $2, $3, $4
        )", [
            uuid.v4(), req.body.name, req.body.trctl, req.body.notes
        ], defer err, result
    if err then done(client); return next(err)
    done()
    res.redirect '/trctl_t'


router.get '/edit/:id', (req, res, next) ->
    res.locals.breadcrumbs.push 'title': '修改交易控制模板'

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "select * from pay_trctl_t
            where serial = $1::int or uuid = $2::char", [
            parseInt(req.params.id) or 0,
            req.params.id
        ], defer err, result
    if err then done(client); return next(err)
    done()

    if result.rows.length != 1
        return next(new Error("查无此交易控制[#{req.params.id}]"))
    record = result.rows[0]
    res.render 'trctl_t/edit', record: record


router.post '/edit', (req, res, next) ->
    for k in ['serial', 'uuid', 'name' ]
        return next(new Error("请求缺少参数[#{k}]")) if not req.body[k]

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "update pay_trctl_t set
            name = $1, trctl = $2, notes = $3
        where serial = $4 and uuid = $5", [
            req.body.name, req.body.trctl, req.body.notes,
            req.body.serial, req.body.uuid
        ], defer err, result
    if err then done(client); return next(err)
    done()
    res.redirect '/trctl_t'


router.post '/delete', (req, res, next) ->
    res.type 'json'
    for k in ['serial', 'uuid']
        return next(new Error("请求缺少参数[#{k}]")) if not req.body[k]

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err);

    await client.query \
        "delete from pay_trctl_t where serial = $1 and uuid = $2", [
            req.body.serial, req.body.uuid,
        ], defer err, result
    if err then done(client); return next(err);
    done()
    res.json succ: true, redirect: '/trctl_t'
